<template>
  <view class="pageContent">
    <view class="list" v-if="isShow && headList.length">
      <view class="listItem" :class="index==headIndex ? 'select' : ''" v-for="(item,index) in headList" :key="index"
        @click="selectHead(index)">
        <image class="houseListIm" :src="localImages +'/houseIcon.png'" />
        <view>{{item.roomInfo || ''}} {{item.roomName}}</view>
      </view>
    </view>
    <view class="empty" v-if="isShow && !headList.length">
      <image class="emptyBac" :src="images +'/emptyHosList.png'" />
      <view class="emptyTip">～未找到相关房屋信息～</view>
      <view class="emptyTip">世纪金源物业客服电话：400-991-5888</view>
    </view>
  </view>
</template>

<script>
  import {
    mapState
  } from 'vuex'
  import config from '../../../../static/config.js'
  import util from '../../../../static/utils/util.js'
  export default {
    data() {
      return {
        images: config.images,
        localImages: config.localImages,
        headList: [],
        headIndex: -1,
        formData: {
          name: "",
          date: "",
          housename: "房屋1",
        },
        isShow: false,
      }
    },
    computed: {
      ...mapState({
        myInfo: state => state.user.myInfo,
        roomlist: state => state.user.roomlist
      })
    },
    onLoad() {
      setTimeout(() => {
        this.isShow = true
      }, 1200)
      console.warn('roomlist', this.roomlist)
      var roomlistTest = [{
        "roleId": 1,
        "ownerId": 32,
        "projectId": 1,
        "roomId": 6,
        "roomName": "房间1601",
        "roomInfo": ""
      }, {}]

      // this.headList=this.roomlist.length ? this.roomlist : roomlistTest
      this.headList = this.roomlist
      // this.noticeList(roomlistTest[0].roomId); //测试选择房屋
      // this.ownerRoomList(); // 不请求接口，直接取缓存的数据
    },
    onShow() {},
    methods: {
      noticeList(room) {
        return new Promise((resolve, reject) => {
          var url = '/UserServer/weChat/ownerLogin'
          var phone = wx.getStorageSync('phone');
          var openId = wx.getStorageSync('openId');
          var userInfo = wx.getStorageSync('userInfo');
          var params = {
            "telephone": phone,
            "roomIdNow": room.roomId,
            "openId": openId,
            "portraitUrl": userInfo.avatarUrl
          }
          util.wxRequestPro(url, params, "POST").then((res) => {
            console.warn('ownerLogin', res)
            var param = res.param
            wx.setStorageSync('room', room);
            wx.setStorageSync('userMsg', param);
            if (param.isDev) {
              wx.setStorageSync('isDev', param.isDev); //内部员工字段标记
            }
            util.socketTest();
            this.handleLogin();
            // wx.switchTab({ url: '/pages/index/main' })
          }).catch((res) => {
            console.warn('catch=ownerLogin', res)
          })
        })
      },
      ownerRoomList() {
        var phoneNumber = wx.getStorageSync('phone');
        //获取业主该手机号绑定的房屋列表
        var params = {
          telephone: phoneNumber
        }
        util.wxRequestPro('/UserServer/room/ownerRoomList', params)
          .then((res) => {
            console.warn('ownerRoomList', res)
            var param = res.param;
            this.headList = param
          }).catch((res) => {
            if (res.statusCode == 400) {
              wx.showToast({
                title: '手机没有绑定房屋',
                icon: 'none'
              })
            }
          })
      },
      selectHead(index) {
        this.headIndex = index;
        var room = this.headList[index]
        this.noticeList(room);
      },
      // 点击登录进行初始化
      handleLogin() {
        var userMsg = wx.getStorageSync('userMsg');
        if (userMsg) {
          var userID = userMsg.im_userId
          // case1: 要登录的用户是当前已登录的用户，则直接跳转即可
          console.warn('this.myInfo', this.myInfo, this.myInfo.userID, userID)
          if (this.myInfo.userID && userID === this.myInfo.userID) {
            wx.reLaunch({
              url: '/pages/index/main'
            })
            return
          }
          // case3: 正常登录流程
          this.login(userMsg)
        } else {
          wx.reLaunch({
            url: '/pages/index/main'
          })
        }
      },
      login(userMsg) {
        console.warn('login=login')
        // var userID = 'user2'
        // let userSig = genTestUserSig(userID).userSig
        var userID = userMsg.im_userId
        var userSig = userMsg.userSig
        wx.$app.login({
          userID: userID,
          userSig: userSig
        }).then((res) => {
          console.warn('login=', res)
          this.$store.commit('setRtcConfig', {
            userID: userID,
            userSig: userSig,
            sdkAppID: wx.$sdkAppID
          })
          wx.reLaunch({
            url: '/pages/index/main'
          })
        }).catch(() => {
          wx.reLaunch({
            url: '/pages/index/main'
          })
        })
      },
    }
  }
</script>

<style lang="stylus" scoped>
  .pageContent {

    padding: 0rpx 0rpx 0rpx 0rpx;
  }

  .list {
    min-height: 100vh;
    padding: 40rpx 30rpx 0rpx 30rpx;
    background: #F3F6F9;
  }

  .listItem {
    display: flex;
    width: 690rpx;
    background: #FFFFFF;
    box-shadow: 0px 2px 4px 0px #E1E2E4;
    margin: 0rpx 0rpx 30rpx 0rpx;
    padding: 30rpx 30rpx 30rpx 30rpx;
    border-radius: 16rpx;
  }

  .empty {
    padding: 200rpx 30rpx 0rpx 30rpx;
  }

  .emptyBac {
    width: 100%;
    height: 432rpx;
    margin-bottom: 100rpx;
  }

  .emptyTip {
    font-size: 36rpx;
    color: #A8AFB6;
    line-height: 56rpx;
    text-align: center;
  }

  .memberMain {
    background-color: #FFFFFF;
    padding: 30rpx 30rpx 20rpx 30rpx;
  }

  .memberType {
    border: 1rpx solid #999999;
  }

  .houseListIm {
    width: 50rpx;
    height: 50rpx;
    margin-right: 30rpx;
  }
</style>
